import 'package:flutter/material.dart';

import 'animated_scale.dart';

///通用点击缩放按钮
///使用时重命名为需要的名字
class AppButton extends StatefulWidget {
  final double height;
  final double width;
  final Widget child;
  final String text;
  final double radius;
  final Color color;
  final TextStyle style;
  final Function() onPressed;

  ///图标
  final Widget icon;

  ///icon是否在末尾
  final bool isIconEnd;

  ///图标与文字的间距
  final double iconSpece;

  ///内边距
  final EdgeInsetsGeometry padding;

  const AppButton({
    Key key,
    this.height,
    this.child,
    this.width,
    @required this.text,
    this.radius,
    this.color,
    this.style,
    this.onPressed,
    this.icon,
    this.isIconEnd = false,
    this.iconSpece = 5,
    this.padding,
  }) : super(key: key);

  @override
  _AppButtonState createState() => _AppButtonState();
}

class _AppButtonState extends State<AppButton> {
  ///缩放
  double _scale;

  @override
  void initState() {
    super.initState();
    _scale = 1;
  }

  @override
  void setState(fn) {
    if (this.mounted) super.setState(fn);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTapDown: (d) {
        if (widget.onPressed != null)
          setState(() {
            _scale = 0.9;
          });
      },
      onTapUp: (d) async {
        if (widget.onPressed != null) {
          widget.onPressed();
          await Future.delayed(const Duration(milliseconds: 100), () {
            setState(() {
              _scale = 1;
            });
          });
        }
      },
      onTapCancel: () async {
        await Future.delayed(const Duration(milliseconds: 200), () {
          setState(() {
            _scale = 1;
          });
        });
      },
      child: AnimatedScale(
        scale: _scale,
        curve: Curves.easeOutCubic,
        duration: const Duration(milliseconds: 200),
        child: Container(
          padding: widget.padding,
          alignment: Alignment.center,
          height: widget.padding == null ? widget.height ?? 60 : null,
          width: widget.width,
          decoration: BoxDecoration(
              color: (widget.color ?? Theme.of(context).accentColor)
                  .withOpacity(widget.onPressed == null ? 0.6 : 1),
              borderRadius: BorderRadius.circular(widget.radius ?? 10)),
          child: widget.child ??
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  if (widget.icon != null && !widget.isIconEnd)
                    Padding(
                      padding: EdgeInsets.only(right: widget.iconSpece),
                      child: widget.icon,
                    ),
                  Text(
                    '${widget.text}',
                    style: widget.style ??
                        TextStyle(
                          color: Theme.of(context).bottomAppBarColor,
                          fontWeight: FontWeight.bold,
                          fontSize: 20,
                          height: 1,
                        ),
                  ),
                  if (widget.icon != null && widget.isIconEnd)
                    Padding(
                      padding: EdgeInsets.only(right: widget.iconSpece),
                      child: widget.icon,
                    ),
                ],
              ),
        ),
      ),
    );
  }
}
